﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharingCenter.Web.Mod.Talks
{
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.Groups;
    public partial class Search : TalksPageBase
    {
        protected string keyword = string.Empty;
        protected string catId = string.Empty;
        protected Guid groupId = Guid.Empty;
        protected Sc_Group group;
        protected Sc_PostCategory _category;

        protected void Page_Init(object sender, EventArgs e)
        {
            try
            {
                keyword = Request.Form["keyword"].Trim();
                catId = Request.Form["searchCategoryId"].Trim();
            }
            catch
            {
                RedirectToDefaultPage();
            }

            if (string.Empty == keyword || string.Empty == catId)
            {

            }

            try
            {
                groupId = new Guid(Request.Form["groupId"]);
                group = GroupOperations.GetGroupByGroupId(groupId);
                
            }
            catch { }
            _category = PostOperations.GetCategory(new Guid(catId));
            LinqDataSource1.Selecting += new EventHandler<LinqDataSourceSelectEventArgs>(LinqDataSource1_Selecting);
            SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(SiteMap_SiteMapResolve);
        }

        protected void Page_InitComplete(object sender, EventArgs e)
        {
            if (group != null)
            {
                SetNav("groups");
            }
        }

        SiteMapNode SiteMap_SiteMapResolve(object sender, SiteMapResolveEventArgs e)
        {
            SiteMapNode currentNode = SiteMap.CurrentNode.Clone(true);
            SiteMapNode tmpNode = currentNode.ParentNode;
            if (null == group)
            {
                tmpNode.Title = _category.CategoryName;
                tmpNode.Url += "?c=" + catId;
            }
            else
            {
                tmpNode.Title = group.GroupName;
                tmpNode.Url = "~/Mod/Groups/ViewGroup.aspx?id=" + group.GroupId;
                tmpNode = tmpNode.ParentNode;
                tmpNode.Title = "圈子";
                tmpNode.Url = "~/Mod/Groups/";
            }
            
            return currentNode;
        }

        protected void Page_Unload(object sender, EventArgs e)
        {
            SiteMap.SiteMapResolve -= new SiteMapResolveEventHandler(this.SiteMap_SiteMapResolve);
        }

        void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            string query = string.Format("EXEC SearchPost '{0}', '{1}'", keyword, catId);
            using (scDataContext cxt = new scDataContext())
            {
                List<ThreadWithMainPost> result = dataContext.ExecuteQuery<ThreadWithMainPost>(query).ToList();
                e.Result = result;
                lblResultCount.Text = result.Count.ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if(group!=null)
            {
                groupHeader1.GroupId = groupId;
            }
        }

        protected void HyperLink2_PreRender(object sender, EventArgs e)
        {
            if (group != null)
            {
                ((HyperLink)sender).NavigateUrl += "&id=" + groupId;
            }
        }
    }
}
